import { jugdyDate } from '../lodashmin'
import { getGlobalData } from '../../configuration/globaldata'

export default class formatdata {
  constructor(entry = {}, userInfo, speciMyMessage) {
    console.log(userInfo, 'userinfo')
    // speciMyMessage 导出的东西更少
    this.userArchive = (entry.archive && (entry.archive || {}).userArchiveModel) || userInfo
    this.userArchiveUserInfo = userInfo
    this.userArchive2 =
      (entry.archive && (entry.archive || {}).jobHuntingModels[0]) || entry.userArchive // 工作机会
    this.userArchiveEducation = entry.education || entry.userArchiveEducation // 学历
    this.userArchiveWork =
      (entry.archive && (entry.archive || {}).userArchiveWorkModels) || entry.userArchiveWork // 工作经历
    this.userArchiveImg = entry.archive && (entry.archive || {}).userArchiveImgModels //  相册
    this.speciMyMessage = speciMyMessage //
  }

  getFormatData() {
    const userInfo = this.getUserInfo()
    const myMessageAndImage = this.getMyMessage()
    const wantWork = this.getWantWork() // 工作机会
    const educationInformation = this.getEducationInformation() // 学历
    const workExp = this.getWorkExp() // 工作经历
    const elegantDemeanour = this.getImgList() // 相册
    console.log(wantWork, 'wantWork')
    return {
      userInfo,
      archiveId: this.userArchive2 && this.userArchive2.archiveId,
      ...myMessageAndImage,
      ...wantWork,
      educationInformation,
      workExp,
      elegantDemeanour,
      ...this.userArchiveUserInfo
    }
  }
  // TODO 寻找工作
  getUserInfo() {
    let intro = this.userArchive && (this.userArchive.intro || this.userArchive.skillDescription)
    const attributes = this.userArchive2
    if (!intro && attributes) {
      const data = attributes
      intro = data.skillDescription
    }
    const jobStatusDic = ['寻找工作', '寻找工作', '考虑机会']
    if (this.userArchive) {
      return {
        ...this.userArchive,
        gender: this.userArchiveUserInfo.gender,
        intro,
        jobStatus: jobStatusDic[this.userArchiveUserInfo.jobStatus || 0],
        age:
          this.userArchive && this.userArchive.age
            ? this.userArchive && this.userArchive.age + '岁'
            : ''
      }
    } else {
      return {}
    }
  }
  getMyMessage() {
    const userArchive = this.userArchive || {}
    let result = []
    if (userArchive.name) {
      result.push({
        name: '姓名',
        value: userArchive.name
      })
    }
    if (userArchive.age && !this.speciMyMessage) {
      result.push({
        name: '年龄',
        value: `${userArchive.age}岁`
      })
    }
    if (userArchive.mobile && !this.speciMyMessage) {
      result.push({
        name: '联系',
        value: userArchive.mobile
      })
    }
    const censusArr = (userArchive && userArchive.census && userArchive.census.split('/')) || null
    const residence =
      (userArchive && userArchive.residence && userArchive.residence.split('/')) || null

    if (censusArr) {
      result.push({
        name: '现居',
        value: `${residence[0] || ''} ${residence[1] || ''}`
      })
    } else if (userArchive.isOld) {
      result.push({
        name: '现居',
        value: `${userArchive.address}`
      })
    }

    if (residence) {
      result.push({
        name: '户籍',
        value: `${censusArr[0] || ''} ${censusArr[1] || ''}`
      })
    }

    return { myMessage: result, userAvator: userArchive.avatarUrl }
  }

  getNameValueParam(attributes) {
    if (!attributes) {
      return {}
    } else {
      let result = {}
      let attributesstr = attributes.replace('CareerObjective(', '').replace(')', '')
      let attributesarr = attributesstr.split(',')
      attributesarr.forEach(data => {
        const dataarr = data.split('=')
        const data0 = dataarr[0] && dataarr[0].replace(' ', '')
        result[data0] = dataarr[1] || ''
      })
      return result
    }
  }

  getWantWork() {
    if (!this.userArchive.extra3) {
      const attributes = this.userArchive2
      if (!attributes) {
        return {
          wantWorkId: null,
          wantWork: []
        }
      } else {
        let data
        console.log(attributes, 'wantWork2')
        // this.getNameValueParam(attributes)
        try {
          data = {...attributes}
        } catch (ex) {}
        if (data) {
          let wantWorkId = data.id
          let wantWork = [
            {
              ...data
            }
          ]
          return { wantWork, wantWorkId }
        } else {
          return {
            wantWorkId: null,
            wantWork: []
          }
        }
      }
    } else {
      // 老数据
      const attributes = this.userArchive
      if (!attributes) {
        return {
          wantWorkId: null,
          wantWork: []
        }
      } else {
        let data
        // this.getNameValueParam(attributes)
        try {
          data = { ...attributes }
        } catch (ex) {}
        if (data) {
          let wantWorkId = data.id
          let wantWork = [
            {
              ...data,
              jobPlace: data.cityName.replace('市', '')
            }
          ]
          return { wantWork, wantWorkId }
        } else {
          return {
            wantWorkId: null,
            wantWork: []
          }
        }
      }
    }
  }

  getEducationInformation() {
    const userArchiveEducation = this.userArchiveEducation
    // 学历列表
    if (!userArchiveEducation) {
      return []
    } else {
      let educationInformationFormat = []
      userArchiveEducation.forEach(data => {
        const course = getGlobalData('education')
        educationInformationFormat.push({
          id: data.id,
          attributes: data.attributes,
          icon: 'https://imagesize.hrwork.com/sp/files/ee62e7ae-ec3a-4e08-98ce-36753fe7ada9.png',
          schoolname: data.schoolName,
          time: `${data.beginTime.split('-')[0]} - ${data.endTime.split('-')[0]}`,
          course:
            course[
              (data.degreeLevel >= 10 ? Math.floor(data.degreeLevel / 10) : data.degreeLevel) - 1
            ].value || '',
          degreeLevel: [...course],
          professional: data.majorName,
          outschool: jugdyDate({ date1: data.endTime }) ? '未毕业' : '已毕业'
        })
      })

      return educationInformationFormat
    }
  }

  getWorkExp() {
    const userArchiveWork = this.userArchiveWork
    if (!userArchiveWork) {
      return []
    } else {
      let userArchiveWorkFormat = []
      userArchiveWork.forEach(data => {
        let beginTimeSplit = data.beginTime.split('-')
        let endTimeSplit = data.endTime.split('-')
        userArchiveWorkFormat.push({
          id: data.id,
          cpname: data.companyName,
          jobtitle: data.positionName,
          beginTime: `${beginTimeSplit[0]}.${beginTimeSplit[1]}`,
          endTime: `${endTimeSplit[0]}.${endTimeSplit[1]}`,
          workcontent: data.achievement || ''
        })
      })
      return userArchiveWorkFormat
    }
  }

  getImgList() {
    const userArchiveImg = this.userArchiveImg
    if (!userArchiveImg) {
      return []
    }
    let elegantDemeanour = []
    userArchiveImg.forEach(data => {
      elegantDemeanour.push({ id: data.id, pic: data.imgUrl, checked: false })
    })
    return elegantDemeanour
  }
}
